/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;

import java.util.Calendar;
import servidorped.Conversation;
import servidorped.ConversationListFactory;
import servidorped.User;

/**
 *
 * @author Micosil
 */
public class message implements Command {

    @Override
    public boolean execute(String[] stringlist, User user) {
        Calendar cur = Calendar.getInstance();
        String currentDate = getDateAsString(cur);
        String rebuiltMessage = "";
        for (int i = 1; i < stringlist.length; i++) {
            rebuiltMessage += stringlist[i];
        }
        Conversation convo = ConversationListFactory.getList().getConversationByID(user.getConversationID()); 
        String completeMessage = "[" + currentDate + "]" + user.getUserName()+ ": "+ rebuiltMessage;
        for (User oneuser : convo.getUsers()) {
            oneuser.getUserSender().setToSend(completeMessage);
        }
        ConversationListFactory.getList().getConversationByID(user.getConversationID()).addLine(completeMessage);
        return true;
    }

    private String getDateAsString(Calendar cur) {
        return Integer.toString(cur.get(Calendar.YEAR)) + "/" + Integer.toString(cur.get(Calendar.MONTH)) + "/" + Integer.toString(cur.get(Calendar.DAY_OF_MONTH)) + " " + Integer.toString(cur.get(Calendar.HOUR_OF_DAY)) + ":" + Integer.toString(cur.get(Calendar.MINUTE)) + ":" + Integer.toString(cur.get(Calendar.SECOND));
    }
}
